$(document).ready(function() {
    $('form').submit(function(event) {
      var productName = $('#productName').val();
      var productPrice = $('#productPrice').val();
      var productCategory = $('#productCategory').val();
      var productStock = $('#productStock').val();
      var productDescription = $('#productDescription').val();
      var productImageFile = $('#productImage')[0].files[0];
  
      if (!productName || !productPrice || !productCategory || !productStock || !productDescription || !productImageFile) {
        alert('所有字段都是必填的！');
        event.preventDefault();
        return;
      }
  
      var imageUrl = URL.createObjectURL(productImageFile);
  
      addProductToList(productName, productPrice, productCategory, productStock, productDescription, imageUrl);
      event.preventDefault();
    });
  
    function addProductToList(productName, productPrice, productCategory, productStock, productDescription, imageUrl) {
      var productList = $('#product-list');
      var html = `
        <tr>
          <td>${productName}</td>
          <td><img src="${imageUrl}" class="product-image" alt="商品图片"></td>
          <td>${productPrice}</td>
          <td>${productCategory}</td>
          <td>${productStock}</td>
          <td>${productDescription}</td>
          <td><button class="btn btn-danger delete-btn">删除</button></td>
        </tr>
      `;
      productList.append(html);
    }
  
    $(document).on('click', '.delete-btn', function() {
      $(this).closest('tr').remove();
    });
  });